# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import csv

# useful for handling different item types with a single interface
import pymongo


# 爬虫只能返回三种
# 请求，进入请求生成器
# item，进入管线
# None，pass
class MongoDBQuotesPipeline:

    def open_spider(self, spider):
        # if spider.name == "quotes_pipline":
        #     print("MongoDB open_spider..........", spider)
        #     self.clent = pymongo.MongoClient()
        #     self.clent.drop_database("quotes_pipline")
        #     self.db = self.clent.get_database("quotes_pipline")
        #     self.collection = self.db.get_collection("pipline")
        #
        #     self.datas = []

        if spider.name == 'quotes':
            self.clent = pymongo.MongoClient()
            self.clent.drop_database("quotes_pipline")
            self.db = self.clent.get_database("quotes_pipline")
            self.collection = self.db.get_collection("pipline")

            self.datas = []

    def process_item(self, item, spider):
        # if spider.name == "quotes_pipline":
        #     # self.collection.insert_one(item)
        #
        #     self.datas.append(item)
        #     if len(self.datas) == 10:
        #         self.collection.insert_many(self.datas)
        #         self.datas.clear()

        if spider.name == 'quotes':
            # print("---------", item, type(item), isinstance(item, QuoteItem))
            self.datas.append({
                "content": item['content'],
                "author": item['author'],
                "tags": item['tags'],

            })
            if len(self.datas) == 10:
                self.collection.insert_many(self.datas)
                self.datas.clear()
        return item

    def close_spider(self, spider):
        # if spider.name == "quotes_pipline":
        #     print("MongoDB close_spider..........", spider)
        #
        #     if self.datas:
        #         self.collection.insert_many(self.datas)
        #         self.datas.clear()
        #     self.clent.close()

        if spider.name == "quotes":
            self.clent.close()


class CSVQuotesPipeline:

    def open_spider(self, spider):
        if spider.name == "quotes_pipline":
            print("CSV open_spider..........", spider)
            self.csv_file = open(f"data.csv", "w", newline="")
            self.csv_writer = csv.writer(self.csv_file)
            self.csv_writer.writerow(["ID", "Content", "Author"])
            self.csv_file_tag = open(f"tag.csv", "w", newline="")
            self.csv_writer_tag = csv.writer(self.csv_file_tag)
            self.csv_writer_tag.writerow(["ID", "Title", "Item_ID"])

            self.item_id = 0
            self.item_tag_id = 0

    def process_item(self, item, spider):
        if spider.name == "quotes_pipline":
            self.csv_writer.writerow([self.item_id, item['content'], item['author']])

            for tag in item['tags']:
                self.csv_writer_tag.writerow([self.item_tag_id, tag, self.item_id])
                self.item_tag_id += 1

            self.item_id += 1

        return item

    def close_spider(self, spider):
        if spider.name == "quotes_pipline":
            print("CSV close_spider..........", spider)
            self.csv_file.close()
            self.csv_file_tag.close()
